package com.sxkj.gis.ignite.client;

import java.util.Arrays;
import java.util.UUID;

import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.DeploymentMode;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.internal.binary.BinaryMarshaller;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;

public class IgniteClient {
	private Ignite ignite;
	private String gridName = "client-node"+UUID.randomUUID().toString();
	
	/**
	 * 启动客户端节点
	 */
	public Ignite StartClient(){
		Ignition.setClientMode(true);
		IgniteConfiguration cfg = new IgniteConfiguration();
		cfg.setPeerClassLoadingEnabled(false);
		cfg.setDeploymentMode(DeploymentMode.SHARED);
		cfg.setMarshaller(new BinaryMarshaller());
		TcpDiscoverySpi tds = new TcpDiscoverySpi();
		tds.setJoinTimeout(2000);//默认是10分钟重连，每个2秒扫描一次，现在改为6000毫秒，连不上就断开；
		TcpDiscoveryVmIpFinder tdv = new TcpDiscoveryVmIpFinder();
		tdv.setAddresses(Arrays.asList("127.0.0.1:47500..47509"));
		tds.setIpFinder(tdv);
		cfg.setDiscoverySpi(tds);
		cfg.setGridName(gridName);
		Ignite ignite = Ignition.start(cfg);
		return ignite;
	}
	
	/**
	 * 关闭指定的客户端节点
	 * @param gridName
	 */
	public  void StopClient(String gridName){
		Ignite ignite = Ignition.ignite(gridName);
		ignite.close();
	}
}
